package com.zaoqi.controller;

import com.zaoqi.pojo.CameraData;
import com.zaoqi.repository.CameraDataRepository;
import com.zaoqi.service.CameraDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/camera")
public class CameraDataController {
    @Autowired
    private CameraDataRepository cameraDataRepository;
    @Autowired
    private CameraDataService cameraDataService;
    /**
     * 获取摄像头实时图片并保存
     * @param deviceId 设备ID
     * @return 返回保存后的 CameraData 对象
     */
    @GetMapping("/capture/{deviceId}")
    public CameraData captureAndSaveImage(@PathVariable String deviceId) {
        return cameraDataService.captureAndSaveCameraImage(deviceId);
    }
    @GetMapping("/api/camera-data")
    public List<CameraDataResponse> getCameraData() {
        // 查询所有 CameraData 数据
        List<CameraData> cameraDataList = cameraDataRepository.findAll();
        // 转换为前端需要的格式，仅保留 photoUrl 和 createdAt
        return cameraDataList.stream()
                .map(data -> new CameraDataResponse(data.getPhotoUrl(), data.getCreatedAt()))
                .collect(Collectors.toList());
    }
    // 定义返回数据格式的内部类
    public static class CameraDataResponse {
        private String photoUrl; // 照片 URL
        private LocalDateTime createdAt; // 创建时间

        public CameraDataResponse(String photoUrl, LocalDateTime createdAt) {
            this.photoUrl = photoUrl;
            this.createdAt = createdAt;
        }

        public String getPhotoUrl() {
            return photoUrl;
        }

        public void setPhotoUrl(String photoUrl) {
            this.photoUrl = photoUrl;
        }

        public LocalDateTime getCreatedAt() {
            return createdAt;
        }

        public void setCreatedAt(LocalDateTime createdAt) {
            this.createdAt = createdAt;
        }
    }
}
